import Vue from "vue";
import VueRouter from "vue-router";
import Home from "../views/Home.vue";
import Login from '../views/Login.vue';
Vue.use(VueRouter);

const routes = [{
    path: "/",
    name: "Home",
    component: Home,
    redirect:"/forms",
    children: [{
        path: "forms",
        name: "Forms",
        component: () => import("../components/Forms.vue"),
      },
      {
        path: "ck",
        name: "Ck",
        component: () => import("../components/Ck.vue"),
      },
      {
        path: "xg",
        name: "Xg",
        component: () => import("../components/Xg.vue"),
      },
      {
        path: "jb",
        name: "Jb",
        component: () => import("../components/Jb.vue"),
      }
    ]
  },
  {
    path: "/login",
    name: "Login",
    component: Login
  },
  {
    path: "/about",
    name: "About",
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () =>
      import( /* webpackChunkName: "about" */ "../views/About.vue"),
  },
];

const router = new VueRouter({
  routes,
});
router.beforeEach((to, from, next) => { 
  console.log(to, from);
  let token = localStorage.getItem("token")
  console.log(token)
  if (to.path === "/login") {
    next()
  } else {
    if (token) { 
      next()
    } else {
      next({
        name: "Login"
      })
    }
  }
})
export default router;